package com.xb.loan.encryptor.handler;


import com.xb.loan.encryptor.conf.HandlerCfg;
import com.xb.loan.encryptor.util.Resources;
import com.xb.loan.util.ObjectHelper;
import com.xb.loan.util.dom4j.Dom4jHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务管理器类
 * 
 * @author heipn
 * @version 1.01
 * 
 */
public class HandlerManagerFactory {

	private final static Log logger = LogFactory.getLog(HandlerManagerFactory.class);

	private static Map<String, HandlerManager> HANDLER_MANAGER_MAP = new HashMap<String, HandlerManager>();
	
	public static final String DEFAULT_HANDLER_VERSION = "1.0";
	
	static{
		
		init("spring/handler.xml");
	}
	/**
	 * 根据版本获得Handler的管理器
	 * 
	 * @param version
	 * @return
	 */
	public static HandlerManager getHandlerManager(String version) {
		return HANDLER_MANAGER_MAP.get(version);
	}

	/**
	 * 初始化方法
	 */
	@SuppressWarnings("unchecked")
	private static synchronized void init(String configPath) {
		InputStream in = null;
		try {
			in = Resources.getResourceAsStream(configPath);
			Document doc = Dom4jHelper.getDocument(in);
			Element root = doc.getRootElement();
			
			List<Node> cfgNodes = Dom4jHelper.getNodes(root, "*");
			for (Node node : cfgNodes) {
				//协议一个handlerManager的节点
				HandlerManagerCfg cfg = new HandlerManagerCfg();
				
				cfg.setClassName(node.selectSingleNode("@className").getText());
				cfg.setVersion(node.selectSingleNode("@version").getText());
				cfg.setHandlers(new ArrayList<HandlerCfg>());
				
				List<Node> nodes = node.selectNodes("handler");
				
				for (Node handlerNode : nodes) {
					
					HandlerCfg handlerCfg = new HandlerCfg();
					cfg.getHandlers().add(handlerCfg);
					
					handlerCfg.setHandlerClass(handlerNode.selectSingleNode("@handlerClass").getText());
					handlerCfg.setTrancode(handlerNode.selectSingleNode("@trancode").getText());
				}
				
				logger.info("HandlerManagerFactory create HandlerManager "
						+ "version is " + cfg.getVersion());
				//根据配置创建handlerManager对象
				HandlerManager manager = (HandlerManager) ObjectHelper
						.simpleNewInstance(cfg.getClassName());
				List<HandlerCfg> handlers = cfg.getHandlers();
				
				if (handlers != null) {
					logger.info("HanlderManager version will add "
							+ handlers.size());
				//根据handlerManager的配置添加相应的handler
					for (HandlerCfg handlerCfg : handlers) {
                        BussinessHandler handler = (BussinessHandler) ObjectHelper.simpleNewInstance(handlerCfg.getHandlerClass());
                        manager.addHandler(handlerCfg.getTrancode(), handler);
                        logger.info(cfg.getVersion() + " HandlerManager add handler " + handlerCfg.getTrancode());
					}
				}
				HANDLER_MANAGER_MAP.put(cfg.getVersion(), manager);
			}
           logger.info("HandlerManagerFactory init finished");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
		}
	}

	public static void main(String[] args) {
		BussinessHandler handler = getHandlerManager("1.0").getHandler("102");
		
		System.out.println(handler);
	}

}
